package com.cl.core.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cl.core.dto.PagingReqDTO;
import com.cl.core.pojo.PlatformRoles;
import com.cl.core.service.impl.RoleServiceImpl;
import com.cl.utility.constant.Constant;
import com.cl.utility.constant.Message;
import com.cl.utility.pojo.ResultCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author liddhappy
 * @description 角色 接口
 * @createTime 2022/12/3 21:23
 */
@RestController
@RequestMapping("/roles")
@Api(tags = "角色接口")
public class RolesController {
    @Autowired
    RoleServiceImpl roleService;

    @PostMapping("/add")
    @ApiOperation(value = "添加角色")
    public ResultCode<PlatformRoles> addRole(@RequestBody PlatformRoles role) {
        roleService.addRole(role);
        return new ResultCode<>(Message.Success(Constant.ADD_SUCCESS),
                roleService.selectRole(role.getRoleId().toString()));
    }

    @DeleteMapping("/delete")
    @ApiOperation(value = "删除角色")
    public ResultCode<PlatformRoles> deleteRole(String roleId) {
        PlatformRoles role = roleService.selectRole(roleId);
        roleService.deleteRole(roleId);
        return new ResultCode<>(Message.Success(Constant.DELETE_SUCCESS), role);
    }

    @PostMapping("/update")
    @ApiOperation(value = "更新角色")
    public ResultCode<PlatformRoles> updateRole(@RequestBody PlatformRoles role) {
        roleService.updateRole(role);
        return new ResultCode<>(Message.Success(Constant.UPDATE_SUCCESS),
                roleService.selectRole(role.getRoleId().toString()));
    }

    @GetMapping("/get")
    @ApiOperation(value = "查询角色")
    public ResultCode<PlatformRoles> getRole(String roleId) {
        PlatformRoles role = roleService.selectRole(roleId);
        return new ResultCode<>(Message.Success(Constant.SELECT_SUCCESS), role);
    }

    /**
     * 分页查找
     *
     * @param reqDTO 分页对象<考试请求>
     * @return 消息对象
     */
    @PostMapping(value = "/paging")
    @ApiOperation(value = "分页查找")
    public ResultCode<IPage<PlatformRoles>> paging(@RequestBody PagingReqDTO<PlatformRoles> reqDTO) {
        //分页查询并转换
        IPage<PlatformRoles> page = roleService.paging(reqDTO);
        return new ResultCode<>(Message.Success(Constant.SELECT_SUCCESS), page);
    }

}
